<?php

namespace App\Model;


use App\Http\Model\Hint;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class SmsAll extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'sms_alls';
    protected $log_tbale = 'sms_all_log';

    /**
     * 新增短信群发
     */
    public function addLists($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/sms/create', $data, 'json', 'POST');

    }

    /**
     * 更改群发短信
     */
    public function smsChange($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/sms/change', $data, 'json', 'POST');
    }

    /**
     * 更改状态值
     */
    public function smsStatusChange($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/sms/statuschange', $data, 'json', 'POST');
    }

    /**
     * 获取单条数据
     */
    public function getOne($smsid)
    {
        $data = DB::connection($this->connection)->table($this->table)->where('id',$smsid)->first();
        $data = objToArray($data);
        return $data;
    }
    /**
     * 获取短信群发列表
     */
    public function getSmsLit($page=1,$limit=10)
    {
        $data = DB::connection($this->connection)->table($this->table)->paginate($limit,'*','page',$page);
        $data = objToArray($data);
        if (!empty($data['data']))
        {
            foreach ($data['data'] as $k=>$v)
            {
                //发送时间戳整理
                $data['data'][$k]['sendtime'] = date('Y-m-d H:i:s',$v['sendtime']);
                $data['data'][$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }
        }
        return $data;
    }

    /**
     * 获取发送短信详情
     * @param int $smsid 短信id
     * @param int $limit 每页条数
     * @param int $page 当前页
     */
    public function getSmsLog($smsid=0,$page=1,$limit=10)
    {
        $data = DB::connection($this->connection)->table($this->log_tbale)->where('smsid', $smsid)->paginate($limit, ['*'], "",$page);
        $data = objToArray($data);
        return $data;
    }

    /**
     * 获取发送失败数据
     * @param $smsid 短信id
     */
    public function downlowData($smsid)
    {
        $data = DB::connection($this->connection)->table($this->log_tbale)->where('smsid',$smsid)->where('status','FAILURE')->get(['phone']);
        $data = objToArray($data);
        return $data;
    }

    /**
     * 立刻发送短信
     * @param $params 参数
     * @param $finalstr 发送的手机号
     * @param $status 短息id
     */
    public function fastSend($params,$finalstr,$status)
    {
        DB::beginTransaction();
        try{
            //队列手机号
            if (!empty($finalstr)) {
                if (!empty($params['phone']))
                {
                    $phonelist = $finalstr . ','.$params['phone'];
                }else{
                    $phonelist = $finalstr;
                }

            } else {
                $phonelist = $params['phone'];
            }

            $phonelist = explode(',',$phonelist);
            $data['phonelist'] = $phonelist;
            $data['content'] = $params['content'];
            //调用第三方短信群发接口
            $sendstatus = curl_request(getKkaudioApiUrl() . '/inner/sms/sendSms', $data, 'json', 'POST');
            if ($sendstatus['code'] != 0)
            {
                //更新当前短信状态
                DB::connection($this->connection)->table($this->table)->where('id',$status['data'])->update(['send_status'=>2,'msgId'=>0]);
                //更新状态表
                $faillist = $phonelist;
                foreach ($faillist as $k=>$v)
                {
                    //数组预处理
                    $insert[$k]['phone'] = $v;
                    $insert[$k]['receiveTime'] = date('Y-m-d H:i',time());
                    $insert[$k]['status'] = 'FAILURE';
                    $insert[$k]['smsid'] = $status['data'];
                }
                DB::connection('api')->table('sms_all_log')->insert($insert);
            }else{
                //短信服务返回的msgid
                $msgId = $sendstatus['data']['msgId'];
                //更新已发送状态
                DB::connection($this->connection)->table($this->table)->where('id',$status['data'])->update(['send_status'=>1,'msgId'=>$msgId]);
            }
            DB::commit();
        }catch (\Exception $e) {
            DB::rollBack();
        }
        return true;
    }
}
